
import java.util.ArrayList;


public class Scout {
        
        static final int CAPTURE_COST = 500;
        static final int DESTROY_COST = 2000;
        
    public static void validateSelectedBlock(XMLDTO vXMLDTO) {//called from main function with an architect request. Must perform all checks, then update overlay and DB
        //checks can be private methods as they will be called locally
        //check if block being captured is activated
        //if block is avtive then--->
        //check if block is a resource (isResource())
        // if is a resource then process resource capture (capture_resource())
        // else check if block is unowned, if it is --> process block capture (capture_block())
        // else if block is owned, then is destroy it (destroy_block())
        // if conditions are met, then check if the block has been previously owned by a team (didExist())
        // if has been owned then reactivate block, if hasnt been owned then activate block (update the DB) (activate_block/reactivate_block() - will call the update overlay)
        
        DatabaseHelper vDatabaseHelper = new DatabaseHelper();
        Scout vScout = new Scout();
        
        if (vScout.isActive (vXMLDTO, vDatabaseHelper) == true && vScout.isResource(vXMLDTO, vDatabaseHelper) == true)
        {
            vScout.captureResource (vXMLDTO);
        }
        
        else
        {
            ArrayList teamList = vDatabaseHelper.returnTeamDTO(vXMLDTO);
            int team_resources = ((TeamDTO) teamList.get(0)).getResources();
            String team_name = ((TeamDTO) teamList.get(0)).getTeamName();
            if (vScout.isActive (vXMLDTO, vDatabaseHelper) == true && vScout.isOwned(vXMLDTO, vDatabaseHelper) == false && team_resources >= DESTROY_COST)
            {
                vScout.destroyBlock(vXMLDTO, team_name ,team_resources);
            }
            else if (vScout.isActive (vXMLDTO, vDatabaseHelper) == true && vScout.isOwned(vXMLDTO, vDatabaseHelper) == false && team_resources >= CAPTURE_COST)
            {   
                vScout.captureBlock(vXMLDTO, team_name ,team_resources);
            }
        }
        
    }
    
    public boolean isActive(XMLDTO vXMLDTO, DatabaseHelper vDatabaseHelper){
        //check that the block being captured is activated by the right team
        //will be false unless the correct team activated it
        boolean state = false;
        ArrayList landList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);
//        for (int i = 0 ; i <= landList.size() - 1 ; i++)
//        {
//            state = (((LandClaimDTO) landList.get(i)).isBlueActive());
//        }
        
        if (landList.isEmpty() == true)
        {
            return false;
        }
        
        if (vXMLDTO.getTeam().equals("Blue"))
        {
            state = (((LandClaimDTO) landList.get(0)).isBlueActive());
        }
        
        if (vXMLDTO.getTeam().equals("Red"))
        {
            state = (((LandClaimDTO) landList.get(0)).isRedActive());
        }
        
        
        return state;
    }
    
    public boolean isResource(XMLDTO vXMLDTO, DatabaseHelper vDatabaseHelper){
        //check that the block being captured is activated by the right team
        ArrayList resourceList = vDatabaseHelper.returnResourceDTO(vXMLDTO);
        if (resourceList.isEmpty())
        {
            return false;
        }
        
        boolean state = (((ResourceDTO) resourceList.get(0)).isHasResources());
        return state;
    }    
    
    public boolean isOwned(XMLDTO vXMLDTO, DatabaseHelper vDatabaseHelper){
        //check that the block being captured is owned by the opposing team
        ArrayList landList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);
        if (landList.isEmpty())
        {
            return false;
        }
        //if true, then the scout's team owns the block already
        boolean state = (((LandClaimDTO) landList.get(0)).getTeamName()).equals(vXMLDTO.getTeam());
      
        return state;
    }    
    
    
    //--------------------------------------------------------------------------

    //public static void captureResource(String Team_Name, int grid_row, int grid_col) {
    public void captureResource(XMLDTO vXMLDTO)
    {
        //update DB and overlay
        DatabaseHelper vDatabaseHelper = new DatabaseHelper();
        ArrayList teamList = vDatabaseHelper.returnTeamDTO(vXMLDTO);
        int current_resources = ((TeamDTO) teamList.get(0)).getResources();
        
        ArrayList resourceList = vDatabaseHelper.returnResourceDTO(vXMLDTO);
        int captured_resources = ((ResourceDTO) resourceList.get(0)).getAmount();
        
        current_resources = current_resources + captured_resources;
        
        vDatabaseHelper.change_resource(((TeamDTO) teamList.get(0)).getTeamName(), current_resources);
        vDatabaseHelper.deactivate_resource(vXMLDTO.getRow(), vXMLDTO.getCol());
    }

    public void destroyBlock(XMLDTO vXMLDTO, String team_name, int team_resources) {
        //update DB and overlay
        DatabaseHelper vDatabaseHelper = new DatabaseHelper();
        
        team_resources = team_resources - DESTROY_COST;
        
        vDatabaseHelper.change_resource(team_name, team_resources);
        vDatabaseHelper.deactivate_resource(vXMLDTO.getRow(), vXMLDTO.getCol());
        
        ArrayList landList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);
        vDatabaseHelper.destroy_block(vXMLDTO);
        
    }

    public void captureBlock(XMLDTO vXMLDTO, String team_name, int team_resources) {
        //update DB and overlay
        DatabaseHelper vDatabaseHelper = new DatabaseHelper();
        
        team_resources = team_resources - CAPTURE_COST;
        
        vDatabaseHelper.change_resource(team_name, team_resources);
        vDatabaseHelper.deactivate_resource(vXMLDTO.getRow(), vXMLDTO.getCol());
        
        ArrayList landList = vDatabaseHelper.returnLandClaimDTO(vXMLDTO);   
        vDatabaseHelper.capture_block(vXMLDTO);
    }
     
}
